<?php

namespace frontend\modules\marketing\controllers;

use frontend\modules\base\controllers\BaseController;
use frontend\modules\marketing\models\CouponUser;
use Yii;
use frontend\modules\marketing\models\Coupon;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * 优惠券信息
 *
 * Class CouponController
 * @package frontend\modules\marketing\controllers
 */
class CouponController extends BaseController
{
    public $enableCsrfValidation = false;

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }


    /**
     * 我的优惠券
     * @desc 我的优惠券信息Mycoupon()
     *
     * @param string $userid |用户ID|yes|40位|
     * @param string $access_token |令牌access_token|yes|32位|
     * @param string data[type] |全部，已使用，未使用,过期|yes|0,1或2|
     *
     * @return string message 提示信息
     * @return string code 错误编码
     * @exception 200 成功获取
     * @exception 400 token验证失败
     * @exception 500 服务器内部错误
     * @return bool|string
     */
    public function actionMycoupon()
    {
        $userinfo = $this->checkAccessToken();
        if(is_object($userinfo)) {
            if($userinfo) {
                $postData = $this->post['data'];
                $time = time();
                if($postData['type']==0) {
                    $CouponUserARR = CouponUser::find()
                        ->select('c.*,cu.is_used')
                        ->from('coupon AS c')
                        ->leftJoin('coupon_user AS cu','c.coupon_id = cu.coupon_id')
                        ->filterWhere(['<', 'c.start_time', $time])
                        ->filterWhere(['>', 'c.end_time', $time])
                        ->AndWhere(['is_used'=>0])
                        ->AndWhere([ 'cu.user_id'=> Yii::$app->UserInfo->userid])->asArray()->all();
                    $message = ['code' => 200, 'message' => '我的未使用优惠券信息获取成功', 'data' => $CouponUserARR];
                    return json_encode($message);
                } elseif($postData['type']==1) {
                    $CouponUserARR = CouponUser::find()
                        ->select('c.*,cu.is_used')
                        ->from('coupon AS c')
                        ->leftJoin('coupon_user AS cu','c.coupon_id = cu.coupon_id')
                        ->AndWhere([ 'cu.user_id'=> Yii::$app->UserInfo->userid])
                        ->AndWhere(['is_used'=>1])->asArray()->all();
                    $message = ['code' => 200, 'message' => '我的已使用优惠券信息获取成功', 'data' => $CouponUserARR];
                    return json_encode($message);
                }elseif($postData['type']==2){
                    $time = time();
                    $CouponUserARR = CouponUser::find()
                        ->select('c.*,cu.is_used')
                        ->from('coupon AS c')
                        ->leftJoin('coupon_user AS cu','c.coupon_id = cu.coupon_id')
                        ->filterWhere(['<', 'c.end_time', $time])
                        ->AndWhere([ 'cu.user_id'=> Yii::$app->UserInfo->userid])
                        ->AndWhere(['is_used'=>0])
                        ->asArray()
                        ->all();
                    $message = ['code' => 200, 'message' => '我的过期优惠券信息获取成功', 'data' => $CouponUserARR];
                    return json_encode($message);
                } else if($postData['type']==3) {
                    $time = time();
                    $CouponUser1ARR = CouponUser::find()
                        ->select('c.*,cu.is_used')
                        ->from('coupon AS c')
                        ->leftJoin('coupon_user AS cu','c.coupon_id = cu.coupon_id')
                        ->filterWhere(['<', 'c.end_time', $time])
                        ->AndWhere([ 'cu.user_id'=> Yii::$app->UserInfo->userid])
                        ->AndWhere(['is_used'=>0])
                        ->asArray()
                        ->all();
                    $CouponUser2ARR = CouponUser::find()
                        ->select('c.*,cu.is_used')
                        ->from('coupon AS c')
                        ->leftJoin('coupon_user AS cu','c.coupon_id = cu.coupon_id')
                        ->AndWhere([ 'cu.user_id'=> Yii::$app->UserInfo->userid])
                        ->AndWhere(['is_used'=>1])->asArray()->all();
                    $message = ['code' => 200, 'message' => '我的过期优惠券信息获取成功', 'data' => array_merge($CouponUser2ARR,$CouponUser1ARR)];
                    return json_encode($message);
                }
            }
        }else{
            return $userinfo;
        }

    }

    /**
     * 领取优惠券
     * @desc 领取优惠券
     *
     * @param string $userid |用户ID|yes|40位|
     * @param string $access_token |令牌access_token|yes|32位|
     * @param string data[couponid] |优惠券ID|yes|40位|
     *
     * @return string message 提示信息
     * @return string code 错误编码
     * @exception 200 成功获取
     * @exception 400 token验证失败
     * @exception 500 服务器内部错误
     * @return bool|string
     */
    public function actionAchivecoupon()
    {
        $userinfo = $this->checkAccessToken();
        if(is_object($userinfo)) {
            if($userinfo){
                $postData = $this->post['data'];
                $couponUser = CouponUser::findOne(['coupon_id'=>$postData['couponid'],'user_id'=> $userinfo->id]);
                if($couponUser){
                    $message = ['code' => 500, 'message' => '领取优惠券失败,您已经领取过此优惠券', 'data' => []];
                    return json_encode($message);
                }
                $couponUser = new CouponUser();
                $couponUser->coupon_id = $postData['couponid'];
                $couponUser->user_id = $userinfo->id;
                $couponUser->number = 1;
                if($couponUser->save()){
                    $message = ['code' => 200, 'message' => '领取优惠券成功', 'data' => []];
                    return json_encode($message);
                }
                $message = ['code' => 500, 'message' => '领取优惠券失败'.json_encode($couponUser->errors), 'data' => []];
                return json_encode($message);
            }
        } else{
            return $userinfo;
        }
    }

    /**
     * 优惠券活动，获取店内所有优惠券
     * @desc 优惠券活动，获取店内优惠券
     *
     * @return string message 提示信息
     * @return string code 错误编码
     * @exception 200 成功获取
     * @exception 400 token验证失败
     * @exception 500 服务器内部错误
     * @return bool|string
     */
    public function actionCoupon()
    {
        $time = time();
        $model = Coupon::find()
            ->select('c.*,cu.coupon_user_id')
            ->from('coupon AS c')
            ->leftJoin('coupon_user AS cu','c.coupon_id = cu.coupon_id')
            ->filterWhere(['<', 'c.start_time', $time])
            ->filterWhere(['>', 'c.end_time', $time])
            ->asArray()
            ->all();

        foreach($model as $k=>$v) {
            $isHave = CouponUser::find()->where(['coupon_id'=>$v['coupon_id'],'user_id'=>Yii::$app->UserInfo->userid])->one();
            if($isHave){
                $v['isHave'] = 1;

            } else {
                $v['isHave'] = 0;
            }
            $model[$k] = $v;
        }

        $message = ['code' => 200, 'message' => '店内优惠券', 'data' => $model];
        return json_encode($message);
    }

    /**
     * 没使用过，已过期的所有优惠券
     * @desc 已过期的所有优惠券
     *
     * @return string message 提示信息
     * @return string code 错误编码
     * @exception 200 成功获取
     * @exception 400 token验证失败
     * @exception 500 服务器内部错误
     * @return bool|string
     */
    public function actionGetcoupon()
    {
        $userid = Yii::$app->UserInfo->userid;
        $time = time();
        $CouponUserARR = CouponUser::find()
            ->select('c.*,cu.is_used')
            ->from('coupon AS c')
            ->leftJoin('coupon_user AS cu','c.coupon_id = cu.coupon_id')
            ->filterWhere(['<', 'c.end_time', $time])
            ->AndWhere([ 'cu.user_id'=> $userid])
            ->AndWhere(['is_used'=>0])
            ->asArray()
            ->all();
        $message = ['code' => 200, 'message' => '我的过期优惠券信息获取成功', 'data' => $CouponUserARR];
        return json_encode($message);
    }

    /**
     * 所有优惠券
     * @desc 所有优惠券
     *
     * @return string message 提示信息
     * @return string code 错误编码
     * @exception 200 成功获取
     * @exception 400 token验证失败
     * @exception 500 服务器内部错误
     * @return bool|string
     */
    public function actionMycouponall()
    {
        $time = time();
        $userid = Yii::$app->UserInfo->userid;
        $CouponUserARR = CouponUser::find()
            ->select('c.*,cu.*')
            ->from('coupon AS c')
            ->leftJoin('coupon_user AS cu','c.coupon_id = cu.coupon_id')
            ->AndWhere([ 'cu.user_id'=> $userid])
            ->AndWhere([ 'cu.is_used'=> 0])
            ->asArray()
            ->all();
        $message = ['code' => 200, 'message' => '我优惠券信息获取成功', 'data' => $CouponUserARR];
        return json_encode($message);
    }
}
